package com.topica.adapter;

import java.util.ArrayList;
import java.util.Hashtable;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.Log;
import android.view.ViewGroup;

import com.topica.object.QuestionItem;
import com.topica.pager.QuestionPager;

public class QuestionAdapter extends FragmentStatePagerAdapter {
	private static final String TAG = "QuestionAdapter";
	private ArrayList<QuestionItem> items;
	private Hashtable<Integer, Fragment> mPageReferenceMap;
	private int mCount;
	
	public QuestionAdapter(FragmentManager fm,ArrayList<QuestionItem> data) {
		super(fm);
		mPageReferenceMap = new Hashtable<Integer, Fragment>();
		items = data;
		mCount = items.size();
	}

	@Override
	public int getCount() {
		return mCount;
	}
	
	@Override
	public Fragment getItem(int position) {
		Fragment fragment = QuestionPager.newInstance();
		Bundle args = new Bundle();
		Log.e(TAG,"items.get(position) = " + items.get(position).getQuestionText());
		args.putInt(QuestionPager.QUESTION_INDEX, position);
		args.putSerializable(QuestionPager.QUESTION_ITEM, items.get(position));
		fragment.setArguments(args);
		mPageReferenceMap.put(Integer.valueOf(position), fragment);
		return fragment;
	}
	
	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		super.destroyItem(container, position, object);
		mPageReferenceMap.remove(Integer.valueOf(position));
	}
	
	public QuestionPager getQuestionPager(int position) {
		return (QuestionPager) mPageReferenceMap.get(position);
	}

}
